home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / cpp / cpp5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  21.0 KB  |  905 lines

  1. /*
  2.  *                C P P 5 . C
  3.  *        E x p r e s s i o n   E v a l u a t i o n
  4.  *
  5.  * Edit History
  6.  * 31-Aug-84    MM    USENET net.sources release
  7.  * 04-Oct-84    MM    __LINE__ and __FILE__ must call ungetstring()
  8.  *            so they work correctly with token concatenation.
  9.  *            Added string formal recognition.
  10.  * 25-Oct-84    MM    "Short-circuit" evaluate #if's so that we
  11.  *            don't print unnecessary error messages for
  12.  *            #if !defined(FOO) && FOO != 0 && 10 / FOO ...
  13.  * 31-Oct-84    ado/MM    Added token concatenation
  14.  *  6-Nov-84    MM    Split from #define stuff, added sizeof stuff
  15.  * 19-Nov-84    ado    #if error returns TRUE for (sigh) compatibility
  16.  * 21-Oct-85    RMS    Rename `token' to `tokenbuf'
  17.  * 23-Oct-85    RMS    Treat undefined symbols as having value zero.
  18.  */
  19.  
  20. #include    <stdio.h>
  21. #include    <ctype.h>
  22. #include    "cppdef.h"
  23. #include    "cpp.h"
  24.  
  25. /*
  26.  * Evaluate an #if expression.
  27.  */
  28.  
  29. static char    *opname[] = {        /* For debug and error messages    */
  30. "end of expression", "val", "id",
  31.   "+",   "-",  "*",  "/",  "%",
  32.   "<<", ">>",  "&",  "|",  "^",
  33.   "==", "!=",  "<", "<=", ">=",  ">",
  34.   "&&", "||",  "?",  ":",  ",",
  35.   "unary +", "unary -", "~", "!",  "(",  ")", "(none)",
  36. };
  37.  
  38. /*
  39.  * opdope[] has the operator precedence:
  40.  *     Bits
  41.  *      7    Unused (so the value is always positive)
  42.  *    6-2    Precedence (000x .. 017x)
  43.  *    1-0    Binary op. flags:
  44.  *        01    The binop flag should be set/cleared when this op is seen.
  45.  *        10    The new value of the binop flag.
  46.  * Note:  Expected, New binop
  47.  * constant    0    1    Binop, end, or ) should follow constants
  48.  * End of line    1    0    End may not be preceeded by an operator
  49.  * binary    1    0    Binary op follows a value, value follows.
  50.  * unary    0    0    Unary op doesn't follow a value, value follows
  51.  *   (        0    0    Doesn't follow value, value or unop follows
  52.  *   )        1    1    Follows value.  Op follows.
  53.  */
  54.  
  55. static char    opdope[OP_MAX] = {
  56.   0001,                    /* End of expression        */
  57.   0002,                    /* Digit            */
  58.   0000,                    /* Letter (identifier)        */
  59.   0141, 0141, 0151, 0151, 0151,        /* ADD, SUB, MUL, DIV, MOD    */
  60.   0131, 0131, 0101, 0071, 0071,        /* ASL, ASR, AND,  OR, XOR    */
  61.   0111, 0111, 0121, 0121, 0121,    0121,    /*  EQ,  NE,  LT,  LE,  GE,  GT    */
  62.   0061, 0051, 0041, 0041, 0031,        /* ANA, ORO, QUE, COL, CMA    */
  63. /*
  64.  * Unary op's follow
  65.  */
  66.   0160, 0160, 0160, 0160,        /* NEG, PLU, COM, NOT        */
  67.   0170, 0013, 0023,            /* LPA, RPA, END        */
  68. };
  69. /*
  70.  * OP_QUE and OP_RPA have alternate precedences:
  71.  */
  72. #define    OP_RPA_PREC    0013
  73. #define OP_QUE_PREC    0034
  74.  
  75. /*
  76.  * S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
  77.  *    #if FOO != 0 && 10 / FOO ...
  78.  * doesn't generate an error message.  They are stored in optab.skip.
  79.  */
  80. #define S_ANDOR        2
  81. #define S_QUEST        1
  82.  
  83. typedef struct optab {
  84.     char    op;            /* Operator            */
  85.     char    prec;            /* Its precedence        */
  86.     char    skip;            /* Short-circuit: TRUE to skip    */
  87. } OPTAB;
  88. static int    evalue;            /* Current value from evallex()    */
  89.  
  90. #ifdef    nomacargs
  91. FILE_LOCAL int
  92. isbinary(op)
  93. register int    op;
  94. {
  95.     return (op >= FIRST_BINOP && op <= LAST_BINOP);
  96. }
  97.  
  98. FILE_LOCAL int
  99. isunary(op)
  100. register int    op;
  101. {
  102.     return (op >= FIRST_UNOP && op <= LAST_UNOP);
  103. }
  104. #else
  105. #define    isbinary(op)    (op >= FIRST_BINOP && op <= LAST_BINOP)
  106. #define    isunary(op)    (op >= FIRST_UNOP  && op <= LAST_UNOP)
  107. #endif
  108.  
  109. /*
  110.  * The following definitions are used to specify basic variable sizes.
  111.  */
  112.  
  113. #ifndef    S_CHAR
  114. #define    S_CHAR        (sizeof (char))
  115. #endif
  116. #ifndef    S_SINT
  117. #define    S_SINT        (sizeof (short int))
  118. #endif
  119. #ifndef    S_INT
  120. #define    S_INT        (sizeof (int))
  121. #endif
  122. #ifndef    S_LINT
  123. #define    S_LINT        (sizeof (long int))
  124. #endif
  125. #ifndef    S_FLOAT
  126. #define    S_FLOAT        (sizeof (float))
  127. #endif
  128. #ifndef    S_DOUBLE
  129. #define    S_DOUBLE    (sizeof (double))
  130. #endif
  131. #ifndef    S_PCHAR
  132. #define    S_PCHAR        (sizeof (char *))
  133. #endif
  134. #ifndef    S_PSINT
  135. #define    S_PSINT        (sizeof (short int *))
  136. #endif
  137. #ifndef    S_PINT
  138. #define    S_PINT        (sizeof (int *))
  139. #endif
  140. #ifndef    S_PLINT
  141. #define    S_PLINT        (sizeof (long int *))
  142. #endif
  143. #ifndef    S_PFLOAT
  144. #define    S_PFLOAT    (sizeof (float *))
  145. #endif
  146. #ifndef    S_PDOUBLE
  147. #define    S_PDOUBLE    (sizeof (double *))
  148. #endif
  149. #ifndef    S_PFPTR
  150. #define S_PFPTR        (sizeof (int (*)()))
  151. #endif
  152.  
  153. typedef struct types {
  154.     short    type;            /* This is the bit if        */
  155.     char    *name;            /* this is the token word    */
  156. } TYPES;
  157.  
  158. static TYPES basic_types[] = {
  159.     { T_CHAR,    "char",        },
  160.     { T_INT,    "int",        },
  161.     { T_FLOAT,    "float",    },
  162.     { T_DOUBLE,    "double",    },
  163.     { T_SHORT,    "short",    },
  164.     { T_LONG,    "long",        },
  165.     { T_SIGNED,    "signed",    },
  166.     { T_UNSIGNED,    "unsigned",    },
  167.     { 0,        NULL,        },    /* Signal end        */
  168. };
  169.  
  170. /*
  171.  * Test_table[] is used to test for illegal combinations.
  172.  */
  173. static short test_table[] = {
  174.     T_FLOAT | T_DOUBLE | T_LONG | T_SHORT,
  175.     T_FLOAT | T_DOUBLE | T_CHAR | T_INT,
  176.     T_FLOAT | T_DOUBLE | T_SIGNED | T_UNSIGNED,
  177.     T_LONG  | T_SHORT  | T_CHAR,
  178.     0                        /* end marker    */
  179. };
  180.  
  181. /*
  182.  * The order of this table is important -- it is also referenced by
  183.  * the command line processor to allow run-time overriding of the
  184.  * built-in size values.  The order must not be changed:
  185.  *    char, short, int, long, float, double (func pointer)
  186.  */
  187. SIZES size_table[] = {
  188.     { T_CHAR,    S_CHAR,        S_PCHAR        },    /* char        */
  189.     { T_SHORT,    S_SINT,        S_PSINT        },    /* short int    */
  190.     { T_INT,    S_INT,        S_PINT        },    /* int        */
  191.     { T_LONG,    S_LINT,        S_PLINT        },    /* long        */
  192.     { T_FLOAT,    S_FLOAT,    S_PFLOAT    },    /* float    */
  193.     { T_DOUBLE,    S_DOUBLE,    S_PDOUBLE    },    /* double    */
  194.     { T_FPTR,    0,        S_PFPTR        },    /* int (*())     */
  195.     { 0,    0,        0        },    /* End of table    */
  196. };
  197.  
  198. int
  199. eval()
  200. /*
  201.  * Evaluate an expression.  Straight-forward operator precedence.
  202.  * This is called from control() on encountering an #if statement.
  203.  * It calls the following routines:
  204.  * evallex    Lexical analyser -- returns the type and value of
  205.  *        the next input token.
  206.  * evaleval    Evaluate the current operator, given the values on
  207.  *        the value stack.  Returns a pointer to the (new)
  208.  *        value stack.
  209.  * For compatiblity with older cpp's, this return returns 1 (TRUE)
  210.  * if a syntax error is detected.
  211.  */
  212. {
  213.     register int    op;        /* Current operator        */
  214.     register int    *valp;        /* -> value vector        */
  215.     register OPTAB    *opp;        /* Operator stack        */
  216.     int        prec;        /* Op precedence        */
  217.     int        binop;        /* Set if binary op. needed    */
  218.     int        op1;        /* Operand from stack        */
  219.     int        skip;        /* For short-circuit testing    */
  220.     int        value[NEXP];    /* Value stack            */
  221.     OPTAB        opstack[NEXP];    /* Operand stack        */
  222.     extern int    *evaleval();    /* Does actual evaluation    */
  223.  
  224.     valp = value;
  225.     opp = opstack;
  226.     opp->op = OP_END;        /* Mark bottom of stack        */
  227.     opp->prec = opdope[OP_END];    /* And its precedence        */
  228.     opp->skip = 0;            /* Not skipping now        */
  229.     binop = 0;
  230. again:    ;
  231. #ifdef    DEBUG_EVAL
  232.     printf("In #if at again: skip = %d, binop = %d, line is: %s",
  233.         opp->skip, binop, infile->bptr);
  234. #endif
  235.     if ((op = evallex(opp->skip)) == OP_SUB && binop == 0)
  236.         op = OP_NEG;            /* Unary minus        */
  237.     else if (op == OP_ADD && binop == 0)
  238.         op = OP_PLU;            /* Unary plus        */
  239.     else if (op == OP_FAIL)
  240.         return (1);                /* Error in evallex    */
  241. #ifdef    DEBUG_EVAL
  242.     printf("op = %s, opdope = %03o, binop = %d, skip = %d\n",
  243.         opname[op], opdope[op], binop, opp->skip);
  244. #endif
  245.     if (op == DIG) {            /* Value?        */
  246.         if (binop != 0) {
  247.         cerror("misplaced constant in #if", NULLST);
  248.         return (1);
  249.         }
  250.         else if (valp >= &value[NEXP-1]) {
  251.         cerror("#if value stack overflow", NULLST);
  252.         return (1);
  253.         }
  254.         else {
  255. #ifdef    DEBUG_EVAL
  256.         printf("pushing %d onto value stack[%d]\n",
  257.             evalue, valp - value);
  258. #endif
  259.         *valp++ = evalue;
  260.         binop = 1;
  261.         }
  262.         goto again;
  263.     }
  264.     else if (op > OP_END) {
  265.         cerror("Illegal #if line", NULLST);
  266.         return (1);
  267.     }
  268.     prec = opdope[op];
  269.     if (binop != (prec & 1)) {
  270.         cerror("Operator %s in incorrect context", opname[op]);
  271.         return (1);
  272.     }
  273.     binop = (prec & 2) >> 1;
  274.     for (;;) {
  275. #ifdef    DEBUG_EVAL
  276.         printf("op %s, prec %d., stacked op %s, prec %d, skip %d\n",
  277.         opname[op], prec, opname[opp->op], opp->prec, opp->skip);
  278. #endif
  279.         if (prec > opp->prec) {
  280.         if (op == OP_LPA)
  281.             prec = OP_RPA_PREC;
  282.         else if (op == OP_QUE)
  283.             prec = OP_QUE_PREC;
  284.         op1 = opp->skip;        /* Save skip for test    */
  285.         /*
  286.          * Push operator onto op. stack.
  287.          */
  288.         opp++;
  289.         if (opp >= &opstack[NEXP]) {
  290.             cerror("expression stack overflow at op \"%s\"",
  291.             opname[op]);
  292.             return (1);
  293.         }
  294.         opp->op = op;
  295.         opp->prec = prec;
  296.         skip = (valp>value) && (valp[-1] != 0);/*Short-circuit tester*/
  297.         /*
  298.          * Do the short-circuit stuff here.  Short-circuiting
  299.          * stops automagically when operators are evaluated.
  300.          */
  301.         if ((op == OP_ANA && !skip)
  302.          || (op == OP_ORO && skip))
  303.             opp->skip = S_ANDOR;    /* And/or skip starts    */
  304.         else if (op == OP_QUE)        /* Start of ?: operator    */
  305.             opp->skip = (op1 & S_ANDOR) | ((!skip) ? S_QUEST : 0);
  306.         else if (op == OP_COL) {    /* : inverts S_QUEST    */
  307.             opp->skip = (op1 & S_ANDOR)
  308.                   | (((op1 & S_QUEST) != 0) ? 0 : S_QUEST);
  309.         }
  310.         else {                /* Other ops leave    */
  311.             opp->skip = op1;        /*  skipping unchanged.    */
  312.         }
  313. #ifdef    DEBUG_EVAL
  314.         printf("stacking %s, valp[-1] == %d at %s",
  315.             opname[op], valp[-1], infile->bptr);
  316.         dumpstack(opstack, opp, value, valp);
  317. #endif
  318.         goto again;
  319.         }
  320.         /*
  321.          * Pop operator from op. stack and evaluate it.
  322.          * End of stack and '(' are specials.
  323.          */
  324.         skip = opp->skip;            /* Remember skip value    */
  325.         switch ((op1 = opp->op)) {        /* Look at stacked op    */
  326.         case OP_END:            /* Stack end marker    */
  327.         if (op == OP_EOE)
  328.             return (valp[-1]);        /* Finished ok.        */
  329.         goto again;            /* Read another op.    */
  330.  
  331.         case OP_LPA:            /* ( on stack        */
  332.         if (op != OP_RPA) {        /* Matches ) on input    */
  333.             cerror("unbalanced paren's, op is \"%s\"", opname[op]);
  334.             return (1);
  335.         }
  336.         opp--;                /* Unstack it        */
  337.         /* goto again;            -- Fall through        */
  338.  
  339.         case OP_QUE:
  340.         goto again;            /* Evaluate true expr.    */
  341.  
  342.         case OP_COL:            /* : on stack.        */
  343.         opp--;                /* Unstack :        */
  344.         if (opp->op != OP_QUE) {    /* Matches ? on stack?    */
  345.             cerror("Misplaced '?' or ':', previous operator is %s",
  346.             opname[opp->op]);
  347.             return (1);
  348.         }
  349.         /*
  350.          * Evaluate op1.
  351.          */
  352.         default:                /* Others:        */
  353.         opp--;                /* Unstack the operator    */
  354. #ifdef    DEBUG_EVAL
  355.         printf("Stack before evaluation of %s\n", opname[op1]);
  356.         dumpstack(opstack, opp, value, valp);
  357. #endif
  358.         valp = evaleval(valp, op1, skip);
  359. #ifdef    DEBUG_EVAL
  360.         printf("Stack after evaluation\n");
  361.         dumpstack(opstack, opp, value, valp);
  362. #endif
  363.         }                    /* op1 switch end    */
  364.     }                    /* Stack unwind loop    */
  365. }
  366.  
  367. FILE_LOCAL int
  368. evallex(skip)
  369. int        skip;        /* TRUE if short-circuit evaluation    */
  370. /*
  371.  * Return next eval operator or value.  Called from eval().  It
  372.  * calls a special-purpose routines for 'char' strings and
  373.  * numeric values:
  374.  * evalchar    called to evaluate 'x'
  375.  * evalnum    called to evaluate numbers.
  376.  */
  377. {
  378.     register int    c, c1, t;
  379.  
  380. again:                      /* Collect the token    */
  381.     c = skipws();
  382.     if ((c = macroid(c)) == EOF_CHAR || c == '\n') {
  383.       unget();
  384.       return (OP_EOE);        /* End of expression    */
  385.     }
  386.     t = type[c];
  387.     if (t == INV) {                /* Total nonsense    */
  388.         if (!skip) {
  389.         if (isascii(c) && isprint(c))
  390.             cierror("illegal character '%c' in #if", c);
  391.         else
  392.             cierror("illegal character (%d decimal) in #if", c);
  393.         }
  394.         return (OP_FAIL);
  395.     }
  396.     else if (t == QUO) {            /* ' or "        */
  397.         if (c == '\'') {            /* Character constant    */
  398.         evalue = evalchar(skip);    /* Somewhat messy    */
  399. #ifdef    DEBUG_EVAL
  400.         printf("evalchar returns %d.\n", evalue);
  401. #endif
  402.         return (DIG);            /* Return a value    */
  403.         }
  404.         cerror("Can't use a string in an #if", NULLST);
  405.         return (OP_FAIL);
  406.     }
  407.     else if (t == LET) {            /* ID must be a macro    */
  408.         if (streq(tokenbuf, "defined")) {    /* Or defined name    */
  409.         c1 = c = skipws();
  410.         if (c == '(')            /* Allow defined(name)    */
  411.             c = skipws();
  412.         if (type[c] == LET) {
  413.             evalue = (lookid(c) != NULL);
  414.             if (c1 != '('        /* Need to balance    */
  415.              || skipws() == ')')    /* Did we balance?    */
  416.             return (DIG);        /* Parsed ok        */
  417.         }
  418.         cerror("Bad #if ... defined() syntax", NULLST);
  419.         return (OP_FAIL);
  420.         }
  421.         else if (streq(tokenbuf, "sizeof"))    /* New sizeof hackery    */
  422.         return (dosizeof());        /* Gets own routine    */
  423.         evalue = 0;
  424.         return (DIG);
  425.     }
  426.     else if (t == DIG) {            /* Numbers are harder    */
  427.         evalue = evalnum(c);
  428. #ifdef    DEBUG_EVAL
  429.         printf("evalnum returns %d.\n", evalue);
  430. #endif
  431.     }
  432.     else if (t == SPA) {
  433.       goto again;
  434.     }
  435.     else if (strchr("=<>&|\\!", c) != NULL) {
  436.         /*
  437.          * Process a possible multi-byte lexeme.
  438.          */
  439.         c1 = cget();            /* Peek at next char    */
  440.         switch (c) {
  441.         case '!':
  442.         if (c1 == '=')
  443.             return (OP_NE);
  444.         break;
  445.  
  446.         case '=':
  447.         if (c1 != '=') {        /* Can't say a=b in #if    */
  448.             unget();
  449.             cerror("= not allowed in #if", NULLST);
  450.             return (OP_FAIL);
  451.         }
  452.         return (OP_EQ);
  453.  
  454.         case '>':
  455.         case '<':
  456.         if (c1 == c)
  457.             return ((c == '<') ? OP_ASL : OP_ASR);
  458.         else if (c1 == '=')
  459.             return ((c == '<') ? OP_LE  : OP_GE);
  460.         break;
  461.  
  462.         case '|':
  463.         case '&':
  464.         if (c1 == c)
  465.             return ((c == '|') ? OP_ORO : OP_ANA);
  466.         break;
  467.  
  468.         case '\\':
  469.         if (c1 == '\n')            /* Multi-line if    */
  470.             goto again;
  471.         cerror("Unexpected \\ in #if", NULLST);
  472.         return (OP_FAIL);
  473.         }
  474.         unget();
  475.     }
  476.     return (t);
  477. }
  478.  
  479. FILE_LOCAL int
  480. dosizeof()
  481. /*
  482.  * Process the sizeof (basic type) operation in an #if string.
  483.  * Sets evalue to the size and returns
  484.  *    DIG        success
  485.  *    OP_FAIL        bad parse or something.
  486.  */
  487. {
  488.     register int    c;
  489.     register TYPES    *tp;
  490.     register SIZES    *sizp;
  491.     register short    *testp;
  492.     short        typecode;
  493.  
  494.     if ((c = skipws()) != '(')
  495.         goto nogood;
  496.     /*
  497.      * Scan off the tokens.
  498.      */
  499.     typecode = 0;
  500.     while ((c = skipws())) {
  501.         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
  502.         goto nogood;            /* End of line is a bug    */
  503.         else if (c == '(') {        /* thing (*)() func ptr    */
  504.         if (skipws() == '*'
  505.          && skipws() == ')') {        /* We found (*)        */
  506.             if (skipws() != '(')    /* Let () be optional    */
  507.             unget();
  508.             else if (skipws() != ')')
  509.             goto nogood;
  510.             typecode |= T_FPTR;        /* Function pointer    */
  511.         }
  512.         else {                /* Junk is a bug    */
  513.             goto nogood;
  514.         }
  515.         }
  516.         else if (type[c] != LET)        /* Exit if not a type    */
  517.         break;
  518.         else {                    /* Maybe combine tokens    */
  519.         /*
  520.          * Look for this unexpandable token in basic_types.
  521.          * The code accepts "int long" as well as "long int"
  522.          * which is a minor bug as bugs go (and one shared with
  523.          * a lot of C compilers).
  524.          */
  525.         for (tp = basic_types; tp->name != NULLST; tp++) {
  526.             if (streq(tokenbuf, tp->name))
  527.             break;
  528.         }
  529.         if (tp->name == NULLST) {
  530.             cerror("#if sizeof, unknown type \"%s\"", tokenbuf);
  531.             return (OP_FAIL);
  532.         }
  533.         typecode |= tp->type;        /* Or in the type bit    */
  534.         }
  535.     }
  536.     /*
  537.      * We are at the end of the type scan.  Chew off '*' if necessary.
  538.      */
  539.     if (c == '*') {
  540.         typecode |= T_PTR;
  541.         c = skipws();
  542.     }
  543.     if (c == ')') {                /* Last syntax check    */
  544.         for (testp = test_table; *testp != 0; testp++) {
  545.         if (!bittest(typecode & *testp)) {
  546.             cerror("#if ... sizeof: illegal type combination", NULLST);
  547.             return (OP_FAIL);
  548.         }
  549.         }
  550.         /*
  551.          * We assume that all function pointers are the same size:
  552.          *        sizeof (int (*)()) == sizeof (float (*)())
  553.          * We assume that signed and unsigned don't change the size:
  554.          *        sizeof (signed int) == (sizeof unsigned int)
  555.          */
  556.         if ((typecode & T_FPTR) != 0)    /* Function pointer    */
  557.         typecode = T_FPTR | T_PTR;
  558.         else {                /* Var or var * datum    */
  559.         typecode &= ~(T_SIGNED | T_UNSIGNED);
  560.         if ((typecode & (T_SHORT | T_LONG)) != 0)
  561.             typecode &= ~T_INT;
  562.         }
  563.         if ((typecode & ~T_PTR) == 0) {
  564.         cerror("#if sizeof() error, no type specified", NULLST);
  565.         return (OP_FAIL);
  566.         }
  567.         /*
  568.          * Exactly one bit (and possibly T_PTR) may be set.
  569.          */
  570.         for (sizp = size_table; sizp->bits != 0; sizp++) {
  571.         if ((typecode & ~T_PTR) == sizp->bits) {
  572.             evalue = ((typecode & T_PTR) != 0)
  573.             ? sizp->psize : sizp->size;
  574.             return (DIG);
  575.         }
  576.         }                    /* We shouldn't fail    */
  577.         cierror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
  578.         return (OP_FAIL);
  579.     }
  580.  
  581. nogood:    unget();
  582.     cerror("#if ... sizeof() syntax error", NULLST);
  583.     return (OP_FAIL);
  584. }
  585.  
  586. FILE_LOCAL int
  587. bittest(value)
  588. /*
  589.  * TRUE if value is zero or exactly one bit is set in value.
  590.  */
  591. {
  592. #if (4096 & ~(-4096)) == 0
  593.     return ((value & ~(-value)) == 0);
  594. #else
  595.     /*
  596.      * Do it the hard way (for non 2's complement machines)
  597.      */
  598.     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
  599. #endif
  600. }
  601.  
  602. FILE_LOCAL int
  603. evalnum(c)
  604. register int    c;
  605. /*
  606.  * Expand number for #if lexical analysis.  Note: evalnum recognizes
  607.  * the unsigned suffix, but only returns a signed int value.
  608.  */
  609. {
  610.     register int    value;
  611.     register int    base;
  612.     register int    c1;
  613.  
  614.     if (c != '0')
  615.         base = 10;
  616.     else if ((c = cget()) == 'x' || c == 'X') {
  617.         base = 16;
  618.         c = cget();
  619.     }
  620.     else base = 8;
  621.     value = 0;
  622.     for (;;) {
  623.         c1 = c;
  624.         if (isascii(c) && isupper(c1))
  625.         c1 = tolower(c1);
  626.         if (c1 >= 'a' && c1 <= 'f')
  627.         c1 -= ('a' - 10);
  628.         else c1 -= '0';
  629.         if (c1 < 0 || c1 >= base)
  630.         break;
  631.         value *= base;
  632.         value += c1;
  633.         c = cget();
  634.     }
  635.     if (c == 'u' || c == 'U')    /* Unsigned nonsense        */
  636.         c = cget();
  637.     unget();
  638.     return (value);
  639. }
  640.  
  641. FILE_LOCAL int
  642. evalchar(skip)
  643. int        skip;        /* TRUE if short-circuit evaluation    */
  644. /*
  645.  * Get a character constant
  646.  */
  647. {
  648.     register int    c;
  649.     register int    value;
  650.     register int    count;
  651.  
  652.     instring = TRUE;
  653.     if ((c = cget()) == '\\') {
  654.         switch ((c = cget())) {
  655.         case 'a':                /* New in Standard    */
  656. #if ('a' == '\a' || '\a' == ALERT)
  657.         value = ALERT;            /* Use predefined value    */
  658. #else
  659.         value = '\a';            /* Use compiler's value    */
  660. #endif
  661.         break;
  662.  
  663.         case 'b':
  664.         value = '\b';
  665.         break;
  666.  
  667.         case 'f':
  668.         value = '\f';
  669.         break;
  670.  
  671.         case 'n':
  672.         value = '\n';
  673.         break;
  674.  
  675.         case 'r':
  676.         value = '\r';
  677.         break;
  678.  
  679.         case 't':
  680.         value = '\t';
  681.         break;
  682.  
  683.         case 'v':                /* New in Standard    */
  684. #if ('v' == '\v' || '\v' == VT)
  685.         value = VT;            /* Use predefined value    */
  686. #else
  687.         value = '\v';            /* Use compiler's value    */
  688. #endif
  689.         break;
  690.  
  691.         case 'x':                /* '\xFF'        */
  692.         count = 3;
  693.         value = 0;
  694.         while ((((c = get()) >= '0' && c <= '9')
  695.              || (c >= 'a' && c <= 'f')
  696.              || (c >= 'A' && c <= 'F'))
  697.             && (--count >= 0)) {
  698.             value *= 16;
  699.             value += (c >= '0' && c <= '9') ?
  700.               (c - '0') : ((c & 0xF) + 9);
  701.         }
  702.         unget();
  703.         break;
  704.  
  705.         default:
  706.         if (c >= '0' && c <= '7') {
  707.             count = 3;
  708.             value = 0;
  709.             while (c >= '0' && c <= '7' && --count >= 0) {
  710.             value *= 8;
  711.             value += (c - '0');
  712.             c = get();
  713.             }
  714.             unget();
  715.         }
  716.         else value = c;
  717.         break;
  718.         }
  719.     }
  720.     else if (c == '\'')
  721.         value = 0;
  722.     else value = c;
  723.     /*
  724.      * We warn on multi-byte constants and try to hack
  725.      * (big|little)endian machines.
  726.      */
  727. #if BIG_ENDIAN
  728.     count = 0;
  729. #endif
  730.     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n') {
  731.         if (!skip)
  732.         ciwarn("multi-byte constant '%c' isn't portable", c);
  733. #if BIG_ENDIAN
  734.         count += BITS_CHAR;
  735.         value += (c << count);
  736. #else
  737.         value <<= BITS_CHAR;
  738.         value += c;
  739. #endif
  740.     }
  741.     instring = FALSE;
  742.     return (value);
  743. }
  744.  
  745. FILE_LOCAL int *
  746. evaleval(valp, op, skip)
  747. register int    *valp;
  748. int        op;
  749. int        skip;        /* TRUE if short-circuit evaluation    */
  750. /*
  751.  * Apply the argument operator to the data on the value stack.
  752.  * One or two values are popped from the value stack and the result
  753.  * is pushed onto the value stack.
  754.  *
  755.  * OP_COL is a special case.
  756.  *
  757.  * evaleval() returns the new pointer to the top of the value stack.
  758.  */
  759. {
  760.     register int    v1, v2;
  761.  
  762.     if (isbinary(op))
  763.         v2 = *--valp;
  764.     v1 = *--valp;
  765. #ifdef    DEBUG_EVAL
  766.     printf("%s op %s", (isbinary(op)) ? "binary" : "unary",
  767.         opname[op]);
  768.     if (isbinary(op))
  769.         printf(", v2 = %d.", v2);
  770.     printf(", v1 = %d.\n", v1);
  771. #endif
  772.     switch (op) {
  773.     case OP_EOE:
  774.          break;
  775.  
  776.     case OP_ADD:
  777.         v1 += v2;
  778.         break;
  779.  
  780.     case OP_SUB:
  781.         v1 -= v2;
  782.         break;
  783.  
  784.     case OP_MUL:
  785.         v1 *= v2;
  786.         break;
  787.  
  788.     case OP_DIV:
  789.     case OP_MOD:
  790.         if (v2 == 0) {
  791.         if (!skip) {
  792.             cwarn("%s by zero in #if, zero result assumed",
  793.             (op == OP_DIV) ? "divide" : "mod");
  794.         }
  795.         v1 = 0;
  796.         }
  797.         else if (op == OP_DIV)
  798.         v1 /= v2;
  799.         else
  800.         v1 %= v2;
  801.         break;
  802.  
  803.     case OP_ASL:
  804.         v1 <<= v2;
  805.         break;
  806.  
  807.     case OP_ASR:
  808.         v1 >>= v2;
  809.         break;
  810.  
  811.     case OP_AND:
  812.         v1 &= v2;
  813.         break;
  814.  
  815.     case OP_OR:
  816.         v1 |= v2;
  817.         break;
  818.  
  819.     case OP_XOR:
  820.         v1 ^= v2;
  821.         break;
  822.  
  823.     case OP_EQ:
  824.         v1 = (v1 == v2);
  825.         break;
  826.  
  827.     case OP_NE:
  828.         v1 = (v1 != v2);
  829.         break;
  830.  
  831.     case OP_LT:
  832.         v1 = (v1 < v2);
  833.         break;
  834.  
  835.     case OP_LE:
  836.         v1 = (v1 <= v2);
  837.         break;
  838.  
  839.     case OP_GE:
  840.         v1 = (v1 >= v2);
  841.         break;
  842.  
  843.     case OP_GT:
  844.         v1 = (v1 > v2);
  845.         break;
  846.  
  847.     case OP_ANA:
  848.         v1 = (v1 && v2);
  849.         break;
  850.  
  851.     case OP_ORO:
  852.         v1 = (v1 || v2);
  853.         break;
  854.  
  855.     case OP_COL:
  856.         /*
  857.          * v1 has the "true" value, v2 the "false" value.
  858.          * The top of the value stack has the test.
  859.          */
  860.         v1 = (*--valp) ? v1 : v2;
  861.         break;
  862.  
  863.     case OP_NEG:
  864.         v1 = (-v1);
  865.         break;
  866.  
  867.     case OP_PLU:
  868.         break;
  869.  
  870.     case OP_COM:
  871.         v1 = ~v1;
  872.         break;
  873.  
  874.     case OP_NOT:
  875.         v1 = !v1;
  876.         break;
  877.  
  878.     default:
  879.         cierror("#if bug, operand = %d.", op);
  880.         v1 = 0;
  881.     }
  882.     *valp++ = v1;
  883.     return (valp);
  884. }
  885.  
  886. #ifdef    DEBUG_EVAL
  887. dumpstack(opstack, opp, value, valp)
  888. OPTAB        opstack[NEXP];    /* Operand stack        */
  889. register OPTAB    *opp;        /* Operator stack        */
  890. int        value[NEXP];    /* Value stack            */
  891. register int    *valp;        /* -> value vector        */
  892. {
  893.     printf("index op prec skip name -- op stack at %s", infile->bptr);
  894.     while (opp > opstack) {
  895.         printf(" [%2d] %2d  %03o    %d %s\n", opp - opstack,
  896.         opp->op, opp->prec, opp->skip, opname[opp->op]);
  897.         opp--;
  898.     }
  899.     while (--valp >= value) {
  900.         printf("value[%d] = %d\n", (valp - value), *valp);
  901.     }
  902. }
  903. #endif
  904.  
  905.